package com.xydtech.ecard.merchant.controller;

import com.alibaba.fastjson.JSONObject;
import com.xydtech.common.annotation.Log;
import com.xydtech.common.core.controller.BaseController;
import com.xydtech.common.core.domain.AjaxResult;
import com.xydtech.common.core.page.TableDataInfo;
import com.xydtech.common.enums.BusinessType;
import com.xydtech.common.utils.DateUtils;
import com.xydtech.common.utils.ShiroUtils;
import com.xydtech.common.utils.poi.ExcelUtil;
import com.xydtech.ecard.merchant.domain.EcardPriorCreditChart;
import com.xydtech.ecard.merchant.domain.EcardPriorCreditDept;
import com.xydtech.ecard.merchant.domain.EcardPriorCreditUser;
import com.xydtech.ecard.merchant.domain.echarts.EcardBaseLineChartEntity;
import com.xydtech.ecard.merchant.domain.echarts.EcardBasePriorChartEntity;
import com.xydtech.ecard.merchant.service.IEcardPriorCreditChartService;
import com.xydtech.system.domain.SysPost;
import com.xydtech.system.service.ISysPostService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * 预授信图表统计
 *
 * @author yuzhiguo
 * @date 2024-05-27 下午4:52
 */
@Controller
@RequestMapping("/merchant/priorChart")
public class EcardPriorCreditChartController extends BaseController {

    @Autowired
    private IEcardPriorCreditChartService ecardPriorCreditChartService;

    @Autowired
    private ISysPostService sysPostService;

    private String prefix = "merchant";

    @GetMapping
    @RequiresPermissions("merchant:branchPriorCredit:view")
    public String mainData(ModelMap mmap) {
        Long userId = ShiroUtils.getUserId();
        Long deptId = ShiroUtils.getSysUser().getDeptId();
        List<SysPost> sysPosts = sysPostService.selectPostByUserId(userId);
        if (sysPosts.size() > 0){
            Long postId = sysPosts.get(0).getPostId();//主岗位id
            if (postId == 2) {
                mmap.put("type", "1");//客户经理
            }
            if (postId == 10) {
                mmap.put("type", "2");//网格管理
            }
        } else {
            mmap.put("type", "1");//客户经理
        }
        mmap.put("priorCreditLimit", ecardPriorCreditChartService.getPriorCreditLimitStatistics(userId));//个人预授信额度统计信息
        mmap.put("priorCreditNum", ecardPriorCreditChartService.getPriorCreditNumStatistics(userId));//个人预授信户数统计信息
        mmap.put("priorCreditAllLimit", ecardPriorCreditChartService.getAllPriorCreditLimit(deptId));//全行预授信额度统计信息
        mmap.put("priorCreditAllNum", ecardPriorCreditChartService.getAllPriorCreditNum(deptId));//全行预授信户数统计信息
        mmap.put("generationTime", DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss", new Date()));
        return prefix + "/branchPriorCredit";
    }

    /**
     * 查询额度折线图数据
     *
     * @return
     */
    @RequestMapping("/getLimitLineChart")
    @ResponseBody
    public Object getLimitLineChart() {
        Long deptId = ShiroUtils.getSysUser().getDeptId();
        //组装图表
        EcardBasePriorChartEntity stackedLineChartEntity = new EcardBasePriorChartEntity();

        //查询额度折线图数据
        List<EcardPriorCreditChart> limitLineChartData = ecardPriorCreditChartService.getLimitLineChart(deptId);
        EcardBasePriorChartEntity.XAxis xAxis = stackedLineChartEntity.getXAxis();

        EcardBasePriorChartEntity.Series filingSeries = new EcardBasePriorChartEntity.Series();
        filingSeries.setName("预授信额度趋势");
        for (EcardPriorCreditChart lineChartDatum : limitLineChartData) {
            filingSeries.getData().add(lineChartDatum.getPriorCreditLimit());
            xAxis.getData().add(lineChartDatum.getMonth());
        }
        stackedLineChartEntity.getSeries().add(filingSeries);
        return JSONObject.toJSON(stackedLineChartEntity);
    }

    /**
     * 查询户数折线图数据
     *
     * @return
     */
    @RequestMapping("/getNumLineChart")
    @ResponseBody
    public Object getNumLineChart() {
        Long deptId = ShiroUtils.getSysUser().getDeptId();
        //组装图表
        EcardBaseLineChartEntity stackedLineChartEntity = new EcardBaseLineChartEntity();

        //查询户数折线图数据
        List<EcardPriorCreditChart> numLineChartData = ecardPriorCreditChartService.getNumLineChart(deptId);
        EcardBaseLineChartEntity.XAxis xAxis = stackedLineChartEntity.getXAxis();

        EcardBaseLineChartEntity.Series filingSeries = new EcardBaseLineChartEntity.Series();
        filingSeries.setName("预授信户数趋势");
        for (EcardPriorCreditChart lineChartDatum : numLineChartData) {
            filingSeries.getData().add(lineChartDatum.getPriorCreditNum());
            xAxis.getData().add(lineChartDatum.getMonth());
        }
        stackedLineChartEntity.getSeries().add(filingSeries);
        return JSONObject.toJSON(stackedLineChartEntity);
    }

    /**
     * 跳转机构预授信情况列表
     * @return
     */
    @GetMapping("/branchPriorCreditDept")
    @RequiresPermissions("merchant:branchPriorCreditDept:view")
    public String branchPriorCreditDept() {
        return prefix + "/branchPriorCreditDept";
    }

    /**
     * 各机构预授信业务排名
     *
     * @param ecardPriorCreditDept
     * @return
     */
    @RequiresPermissions("merchant:branchPriorCreditDept:list")
    @PostMapping("/getDeptPriorCreditList")
    @ResponseBody
    public TableDataInfo getDeptPriorCreditList(EcardPriorCreditDept ecardPriorCreditDept) {
        startPage();
        List<EcardPriorCreditDept> list = ecardPriorCreditChartService.getDeptPriorCreditList(ecardPriorCreditDept);
        return getDataTable(list);
    }

    /**
     * 导出各机构预授信业务排名
     *
     * @param ecardPriorCreditDept
     * @return
     */
    @RequiresPermissions("merchant:branchPriorCreditDept:export")
    @Log(title = "导出各机构预授信业务排名", businessType = BusinessType.EXPORT)
    @PostMapping("/exportDeptPriorCreditList")
    @ResponseBody
    public AjaxResult exportDeptPriorCreditList(EcardPriorCreditDept ecardPriorCreditDept) {
        List<EcardPriorCreditDept> list = ecardPriorCreditChartService.getDeptPriorCreditList(ecardPriorCreditDept);
        ExcelUtil<EcardPriorCreditDept> util = new ExcelUtil<EcardPriorCreditDept>(EcardPriorCreditDept.class);
        return util.exportExcel(list, "各机构预授信业务排名");
    }

    /**
     * 各机构个人预授信业务排名
     *
     * @param ecardPriorCreditDept
     * @return
     */
    @RequiresPermissions("merchant:branchPriorCreditDept:list")
    @PostMapping("/getDeptAnthropoidsPriorCreditList")
    @ResponseBody
    public TableDataInfo getDeptAnthropoidsPriorCreditList(EcardPriorCreditDept ecardPriorCreditDept) {
        ecardPriorCreditDept.setCustType("1");//个人
        startPage();
        List<EcardPriorCreditDept> list = ecardPriorCreditChartService.getDeptPriorCreditList(ecardPriorCreditDept);
        return getDataTable(list);
    }

    /**
     * 导出各机构个人预授信业务排名
     *
     * @param ecardPriorCreditDept
     * @return
     */
    @RequiresPermissions("merchant:branchPriorCreditDept:export")
    @Log(title = "导出各机构个人预授信业务排名", businessType = BusinessType.EXPORT)
    @PostMapping("/exportDeptAnthropoidsPriorCreditList")
    @ResponseBody
    public AjaxResult exportDeptAnthropoidsPriorCreditList(EcardPriorCreditDept ecardPriorCreditDept) {
        ecardPriorCreditDept.setCustType("1");//个人
        List<EcardPriorCreditDept> list = ecardPriorCreditChartService.getDeptPriorCreditList(ecardPriorCreditDept);
        ExcelUtil<EcardPriorCreditDept> util = new ExcelUtil<EcardPriorCreditDept>(EcardPriorCreditDept.class);
        return util.exportExcel(list, "各机构个人预授信业务排名");
    }

    /**
     * 各机构商户预授信业务排名
     *
     * @param ecardPriorCreditDept
     * @return
     */
    @RequiresPermissions("merchant:branchPriorCreditDept:list")
    @PostMapping("/getDeptMerchantPriorCreditList")
    @ResponseBody
    public TableDataInfo getDeptMerchantPriorCreditList(EcardPriorCreditDept ecardPriorCreditDept) {
        ecardPriorCreditDept.setCustType("2");//商户
        startPage();
        List<EcardPriorCreditDept> list = ecardPriorCreditChartService.getDeptPriorCreditList(ecardPriorCreditDept);
        return getDataTable(list);
    }

    /**
     * 导出各机构商户预授信业务排名
     *
     * @param ecardPriorCreditDept
     * @return
     */
    @RequiresPermissions("merchant:branchPriorCreditDept:export")
    @Log(title = "导出各机构商户预授信业务排名", businessType = BusinessType.EXPORT)
    @PostMapping("/exportDeptMerchantPriorCreditList")
    @ResponseBody
    public AjaxResult exportDeptMerchantPriorCreditList(EcardPriorCreditDept ecardPriorCreditDept) {
        ecardPriorCreditDept.setCustType("2");//商户
        List<EcardPriorCreditDept> list = ecardPriorCreditChartService.getDeptPriorCreditList(ecardPriorCreditDept);
        ExcelUtil<EcardPriorCreditDept> util = new ExcelUtil<EcardPriorCreditDept>(EcardPriorCreditDept.class);
        return util.exportExcel(list, "各机构商户预授信业务排名");
    }

    /**
     * 跳转营销人员预授信情况列表
     * @return
     */
    @GetMapping("/branchPriorCreditUser")
    @RequiresPermissions("merchant:branchPriorCreditUser:view")
    public String branchPriorCreditUser() {
        return prefix + "/branchPriorCreditUser";
    }

    /**
     * 各营销人员预授信业务排名
     *
     * @param ecardPriorCreditUser
     * @return
     */
    @RequiresPermissions("merchant:branchPriorCreditUser:list")
    @PostMapping("/getUserPriorCreditList")
    @ResponseBody
    public TableDataInfo getUserPriorCreditList(EcardPriorCreditUser ecardPriorCreditUser) {
        startPage();
        List<EcardPriorCreditUser> list = ecardPriorCreditChartService.getUserPriorCreditList(ecardPriorCreditUser);
        return getDataTable(list);
    }

    /**
     * 导出各营销人员预授信业务排名
     *
     * @param ecardPriorCreditUser
     * @return
     */
    @RequiresPermissions("merchant:branchPriorCreditUser:export")
    @Log(title = "导出各营销人员预授信业务排名", businessType = BusinessType.EXPORT)
    @PostMapping("/exportUserPriorCreditList")
    @ResponseBody
    public AjaxResult exportUserPriorCreditList(EcardPriorCreditUser ecardPriorCreditUser) {
        List<EcardPriorCreditUser> list = ecardPriorCreditChartService.getUserPriorCreditList(ecardPriorCreditUser);
        ExcelUtil<EcardPriorCreditUser> util = new ExcelUtil<EcardPriorCreditUser>(EcardPriorCreditUser.class);
        return util.exportExcel(list, "各营销人员预授信业务排名");
    }

    /**
     * 各营销人员个人预授信业务排名
     *
     * @param ecardPriorCreditUser
     * @return
     */
    @RequiresPermissions("merchant:branchPriorCreditUser:list")
    @PostMapping("/getUserAnthropoidsPriorCreditList")
    @ResponseBody
    public TableDataInfo getUserAnthropoidsPriorCreditList(EcardPriorCreditUser ecardPriorCreditUser) {
        ecardPriorCreditUser.setCustType("1");//个人
        startPage();
        List<EcardPriorCreditUser> list = ecardPriorCreditChartService.getUserPriorCreditList(ecardPriorCreditUser);
        return getDataTable(list);
    }

    /**
     * 导出各营销人员个人预授信业务排名
     *
     * @param ecardPriorCreditUser
     * @return
     */
    @RequiresPermissions("merchant:branchPriorCreditUser:export")
    @Log(title = "导出各营销人员个人预授信业务排名", businessType = BusinessType.EXPORT)
    @PostMapping("/exportUserAnthropoidsPriorCreditList")
    @ResponseBody
    public AjaxResult exportUserAnthropoidsPriorCreditList(EcardPriorCreditUser ecardPriorCreditUser) {
        ecardPriorCreditUser.setCustType("1");//个人
        List<EcardPriorCreditUser> list = ecardPriorCreditChartService.getUserPriorCreditList(ecardPriorCreditUser);
        ExcelUtil<EcardPriorCreditUser> util = new ExcelUtil<EcardPriorCreditUser>(EcardPriorCreditUser.class);
        return util.exportExcel(list, "各营销人员个人预授信业务排名");
    }

    /**
     * 各营销人员商户预授信业务排名
     *
     * @param ecardPriorCreditUser
     * @return
     */
    @RequiresPermissions("merchant:branchPriorCreditUser:list")
    @PostMapping("/getUserMerchantPriorCreditList")
    @ResponseBody
    public TableDataInfo getUserMerchantPriorCreditList(EcardPriorCreditUser ecardPriorCreditUser) {
        ecardPriorCreditUser.setCustType("2");//商户
        startPage();
        List<EcardPriorCreditUser> list = ecardPriorCreditChartService.getUserPriorCreditList(ecardPriorCreditUser);
        return getDataTable(list);
    }

    /**
     * 导出各营销人员商户预授信业务排名
     *
     * @param ecardPriorCreditUser
     * @return
     */
    @RequiresPermissions("merchant:branchPriorCreditUser:export")
    @Log(title = "导出各营销人员商户预授信业务排名", businessType = BusinessType.EXPORT)
    @PostMapping("/exportUserMerchantPriorCreditList")
    @ResponseBody
    public AjaxResult exportUserMerchantPriorCreditList(EcardPriorCreditUser ecardPriorCreditUser) {
        ecardPriorCreditUser.setCustType("2");//商户
        List<EcardPriorCreditUser> list = ecardPriorCreditChartService.getUserPriorCreditList(ecardPriorCreditUser);
        ExcelUtil<EcardPriorCreditUser> util = new ExcelUtil<EcardPriorCreditUser>(EcardPriorCreditUser.class);
        return util.exportExcel(list, "各营销人员商户预授信业务排名");
    }
}
